var mysql = require('mysql2')
var mysqlConn = require('../conf/mysql.js').mysql
var pool = mysql.createPool(mysqlConn)

exports.getCount = (date, count) => {
  let promise = new Promise((resolve, reject) => {
  let  qryString = 'SELECT * FROM mini_stock_cnt WHERE trade_date<=\''+ date+ '\' order by trade_date DESC LIMIT '+ count;

    pool.query(qryString, (err, results, fields) => {
      if (err) {
        reject(err)
      } else {
        resolve({
          data: results
        })
      }
    })
  })
  return promise
}

exports.getStock = (date, type) => {
  if (type === 'market') {
    var qryString = 'SELECT stock_code FROM mini_market WHERE trade_date=\''+date+'\' AND market_value<2500000000';
  }else if (type ==='amount') {
    var qryString = 'SELECT stock_code FROM mini_market WHERE trade_date='+date+' AND amount<5000000 and low_price!=high_price';
  }else if(type==='same'){
    var qryString = 'SELECT stock_code FROM mini_market WHERE trade_date='+date+' AND amount<5000000 and low_price!=high_price and market_value<2500000000';
  }
  console.log('qry',qryString);

  let promise = new Promise((resolve, reject) => {
    pool.query(qryString, (err, results, fields) => {
      if (err) {
        reject(err)
      } else {
        resolve({
          data: results
        })
      }
    })
  })
  return promise
}